package com.whynot.frame.ms.controller;

import com.whynot.frame.auth.shiro.annotation.CurrentUser;
import com.whynot.frame.basic.common.model.APIBase;
import com.whynot.frame.basic.common.model.ErrorCode;
import com.whynot.frame.basic.util.YmlUtil;
import com.whynot.frame.ms.entities.SysUser;
import com.whynot.frame.ms.service.UserService;
import com.whynot.frame.ms.service.impl.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

/**
 * 描    述:校验、测试接口
 * 创建时间: 2018/07/10
 */
@Api(value = "test",description = "测试接口")
@RestController
@RequestMapping("/test")
@RefreshScope
public class TestController {

    @Autowired
    UserServiceImpl userService;

    @ApiOperation(value = "是否拥有dept:add权限", notes = "是否拥有dept:add权限", response = APIBase.class)
    @GetMapping("/require_auth")
    @RequiresPermissions("dept:add")
    public APIBase requireAuth() {
        return new APIBase(ErrorCode.SUCCESS_CODE, "You are authenticated", "拥有dept:add权限",true);
    }

    @ApiOperation(value = "是否拥有role:add权限", notes = "是否拥有role:add权限", response = APIBase.class)
    @GetMapping("/requireAuth3")
    @RequiresPermissions("role:add")
    public APIBase requireAuth3() {
        return new APIBase(ErrorCode.SUCCESS_CODE, "You are authenticated", "拥有role:add权限",true);
    }

    @ApiOperation(value = "是否拥有admin角色", notes = "是否拥有dept:add权限", response = APIBase.class)
    @GetMapping("/require_auth2")
    @RequiresRoles("admin")
    public APIBase requireAuth2() {
        return new APIBase(ErrorCode.SUCCESS_CODE, "You are authenticated2", "拥有admin角色",true);
    }

    @ApiOperation(value = "是否已认证", notes = "是否已认证", response = APIBase.class)
    @GetMapping(value = "/article")
    @ResponseBody
    public APIBase article() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            return new APIBase(ErrorCode.SUCCESS_CODE, "You are already logged in", null,true);
        } else {
            return new APIBase(ErrorCode.ERR_UN_TOKEN, "You are guest", null,true);
        }
    }

    @ApiOperation(value = "验证是否所有接口都需要认证", notes = "验证是否所有接口都需要认证", response = APIBase.class)
    @GetMapping(path = "/index")
    @ResponseBody
    public APIBase index() {
        return new APIBase(ErrorCode.ERR_UN_TOKEN, "index", null,true);
    }

    @ApiOperation(value = "获取当前用户", notes = "获取当前用户", response = APIBase.class)
    @GetMapping(path = "/index2")
    @ResponseBody
    public APIBase index2(@CurrentUser SysUser user) {
        return new APIBase(ErrorCode.SUCCESS_CODE, "index", user,true);
    }


    @Autowired
    YmlUtil ymlUtil;

    Environment environment;


    @Value("${uiService.systemService}")
    private String uiService;

    @GetMapping(path = "/testggg")
    @ResponseBody
    public APIBase test(){
        ymlUtil.getListByKey("uiService");
        return  new APIBase();
    }

    @GetMapping(path = "/testggg2")
    @ResponseBody
    public APIBase test2(){
        try {
            userService.insertTest();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return  new APIBase();
    }
}